clear all; close all; clc

model1Path = 'O:\PROJ_LIB\LJM\exog\CEFJ\BigARMAHP\Prior12 Gap OverIdent HP FL\02 03 12';
model2Path = 'O:\PROJ_LIB\LJM\exog\CEFJ\BigARMAHP\Prior9 Gap OverIdent HP FL\02 03 12 ';

modelNames = {'Prior 12', 'Prior 9'};

cwd = cd;

%% Extract Model 1
cd(model1Path)
load workspace

% Retrieve names for shocks and observables
[GG, RR, CONS, eu, SDX, ZZ, initss, ssvec,~, ssNames,stateNames,shockNamesStru]...
        =feval(funcmod,parMode,solveopt,addsol);
shockNames=shockNamesStru.long; 
[Znames,ZposInStates]=extractObsNames(ZZ(:,:,1),stateNames);

% Preallocate matrices to hold counterfactuals and innovations
countStMat   = nan([size(KFStru.countSt(:,report.stateDecompPos,:)) 2]); %[T NS NX NM]
innovationMat = nan([size(KFStru.innovations) 2]); %[T NX NM]

% Fill first part with model 1 data
countStMat(:,:,:,1) = KFStru.countSt(:,report.stateDecompPos,:);
innovationsMat(:,:,1) = KFStru.innovations;

ns = size(countStMat,2);
nx = size(countStMat,3);

%% Extract Model 2
cd(model2Path)
load workspace

% Fill second part with model 2 data
countStMat(:,:,:,2) = KFStru.countSt(:,report.stateDecompPos,:);
innovationsMat(:,:,2) = KFStru.innovations;

%% Plot the counterfactuals
gvec = zeros(ns,1);

plotNames.colNames = report.stateDecomp;
plotNames.pageNames = modelNames;

plotStruct.legend = modelNames;
plotStruct.xaxis = sampleSS.sampleVec;
plotStruct.lineWidths = [1.5 1.7];
plotStruct.NFigRows = 4;
plotStruct.NFigCols = 3;
for k = 1:nx
    
    plotStruct.suptitle = shockNames{k};
    varMat = squeeze(countStMat(:,:,k,:));
    gvec(k) = plotColForAllPages(varMat,plotStruct,plotNames);
    suptitle(shockNames{k},14)
    set(gvec(k),'paperPosition',[0.25 0.4 8 10])

end

plots2PDF(gvec,'countSt',cwd);
%% Plot Innovations

plotNames.colNames = shockNames;
plotNames.pageNames = modelNames;
plotStruct.legend = modelNames;
plotStruct.xaxis = sampleSS.sampleVec;
plotStruct.NFigRows = 3;
plotStruct.NFigCols =2;
plotStruct.suptitle = 'Innovations';

gvec = plotColForAllPages(innovationsMat,plotStruct,plotNames);

plots2PDF(gvec,'innovations',cwd);
close all;